
local str = [[
    ------  -----  --------------------------------------------------  ----------------  ------------------
    33.2  OUT    2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  ****************         5.1.0
                 2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  ****************         5.1.16
                 2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  ****************         5.1.32
                 2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  2a 2a 2a 2a  ****************         5.1.48
    33.2  OUT    2a 2b 2a 2b  2a 2b 2a 2b  2a 2b 28 29  2a 2b 2a 2b  *+*+*+*+*+()*+*+        30.1.0
                 28 29 28 29  28 81 06 28  29 2a 2b 2a  2b 28 29 28  ()()(..()*+*+()(        30.1.16
                 8c 00 00 00  00 00 00 00  00 00 00 00  00 28 29 28  .............()(        30.1.32
                 29 28 29 28  29 2a 2b 28  29 28 29 28  c4 00 00 00  )()()*+()()(....        30.1.48
                 00 28 69 28  69 28 69 28  69 28 69 28  69 28 69 2a  .(i(i(i(i(i(i(i*        30.1.64
                 6b 2a 2b 28  29                                     k*+()                   30.1.80
    33.2  OUT    2a 2b 2a 2b  28 29 28 29  28 81 e3 38  39 3a 3b 2a  *+*+()()(..89:;*        35.1.0
                 2b 28 29 28  bf 00 00 00  00 00 00 00  00 00 00 00  +()(............        35.1.16
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.32
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.48
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.64
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.80
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.96
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.112
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.128
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.144
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.160
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.176
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.192
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.208
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.224
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.240
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.256
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.272
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.288
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.304
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.320
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.336
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.352
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.368
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.384
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.400
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.416
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.432
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.448
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.464
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.480
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.496
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.512
                 00 00 00 00  bf 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.528
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.544
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.560
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.576
                 00 00 00 00  ba 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.592
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.608
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................        35.1.624
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 2a  ...............*        35.1.640
                 2b 2a 2b 28  29 28 29 28  81 f8 38 39  3a 3b 2a 2b  +*+()()(..89:;*+        35.1.656
                 28 29 28 8c  00 00 00 00  00 00 00 00  00 00 00 00  ()(.............        35.1.672
                 28 29 28 29  28 29 28 29  2a 2b 2a 2b  28 29 28 29  ()()()()*+*+()()        35.1.688
                 28 81 fc 38  39 3a 3b 2a  2b 28 29 28  8c 00 00 00  (..89:;*+()(....        35.1.704
                 00 00 00 00  00 00 00 00  00 28 29 28  29 28 29 28  .........()()()(        35.1.720
                 29 2a 2b 28  29 28 29 28  29 28 29 28  29 28 29 38  )*+()()()()()()8        35.1.736
                 39 38 39 38  39 3a 3b 2a  2b 28 29 2a  2b 2a 2b 28  98989:;*+()*+*+(        35.1.752
                 29 28 29 28  81 fa 38 39  3a 3b 2a 2b  28 29 28 8c  )()(..89:;*+()(.        35.1.768
                 00 00 00 00  00 00 00 00  00 00 00 00  28 29 28 29  ............()()        35.1.784
                 28 29 28 29  2a 2b 28 29  28 29 38 39  38 39 28 29  ()()*+()()8989()        35.1.800
                 28 29 28 29  38 39 38 39  3a 3b 2a 2b  28 29 2a 2b  ()()8989:;*+()*+        35.1.816
                 2a 2b 28 29  28 29 28 81  f9 38 39 3a  3b 2a 2b 28  *+()()(..89:;*+(        35.1.832
                 29 28 8c 00  00 00 00 00  00 00 00 00  00 00 00 28  )(.............(        35.1.848
                 29 28 29 28  29 28 29 2a  2b 2a 2b 28  29 28 29 28  )()()()*+*+()()(        35.1.864
                 81 f8 38 39  3a 3b 2a 2b  28 29 28 8c  00 00 00 00  ..89:;*+()(.....        35.1.880
                 00 00 00 00  00 00 00 00  28 29 28 29  28 29 28 29  ........()()()()        35.1.896
                 2a 2b 2a 2b  28 29 28 29  28 81 fc 38  39 3a 3b 2a  *+*+()()(..89:;*        35.1.912
                 2b 28 29 28  8c 00 00 00  00 00 00 00  00 00 00 00  +()(............        35.1.928
                 00 28 29 28  29 28 29 28  29 2a 2b 28  29 28 29 28  .()()()()*+()()(        35.1.944
                 29 28 29 28  29 28 29 38  39 38 39 38  39 3a 3b 2a  )()()()898989:;*        35.1.960
                 2b 28 29 2a  2b 2a 2b 28  29 28 29 28  81 fd 38 39  +()*+*+()()(..89        35.1.976
                 3a 3b 2a 2b  28 29 28 8c  00 00 00 00  00 00 00 00  :;*+()(.........        35.1.992
                 00 00 00 00  28 29 28 29  28 29 28 29  2a 2b 28 29  ....()()()()*+()        35.1.1008
    33.2  OUT    c1 00                                               ..                      37.1.0(27)
    33.2  OUT    c1 00                                               ..                      91.1.0
    33.2  OUT    c1 00                                               ..                      94.1.0(35)
    33.2  OUT    c1 00                                               ..                     165.1.0(27)
    33.2  OUT    2a 2b 2a 2b  28 29 2a 2b  2a 2b 28 29  28 29 28 81  *+*+()*+*+()()(.       220.1.0
                 e3 38 39 3a  3b 2a 2b 28  29 28 bf 00  00 00 00 00  .89:;*+()(......       220.1.16
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.32
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.48
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.64
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.80
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.96
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.112
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.128
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.144
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.160
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.176
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.192
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.208
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.224
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.240
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.256
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.272
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.288
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.304
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.320
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.336
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.352
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.368
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.384
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.400
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.416
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.432
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.448
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.464
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.480
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.496
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.512
                 00 00 00 00  00 00 00 00  00 00 bf 00  00 00 00 00  ................       220.1.528
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.544
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.560
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.576
                 00 00 00 00  00 00 00 00  00 00 ba 00  00 00 00 00  ................       220.1.592
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.608
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.624
                 00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00  ................       220.1.640
                 00 00 00 00  00                                     .....                  220.1.656
    33.2  OUT    0a                                                  .                      221.1.0

]];

local format = string.format;
local JTAG_TABLE = {};
JTAG_TABLE.LRESET = {'RUNTEST','LRESET'};
JTAG_TABLE.RUNTEST = {'RUNTEST','SELECT_DR'};
JTAG_TABLE.SELECT_DR = {'CAP_DR','SELECT_IR'};
JTAG_TABLE.SELECT_IR = {'CAP_IR','LRESET'}
JTAG_TABLE.CAP_DR = {'SHIFT_DR','EXIT_DR'};
JTAG_TABLE.CAP_IR = {'SHIFT_IR','EXIT_IR'};
JTAG_TABLE.SHIFT_DR = {'SHIFT_DR','EXIT_DR'};
JTAG_TABLE.SHIFT_IR = {'SHIFT_IR','EXIT_IR'};
JTAG_TABLE.EXIT_DR = {'PAUSE_DR','UPDATE_DR'};
JTAG_TABLE.EXIT_IR = {'PAUSE_IR','UPDATE_IR'};
JTAG_TABLE.PAUSE_DR = {'PAUSE_DR','EXIT2_DR'};
JTAG_TABLE.PAUSE_IR = {'PAUSE_IR','EXIT2_IR'};
JTAG_TABLE.EXIT2_DR = {'SHIFT_DR','UPDATE_DR'};
JTAG_TABLE.EXIT2_IR = {'SHIFT_IR','UPDATE_IR'};
JTAG_TABLE.UPDATE_DR = {'RUNTEST','SELECT_DR'};
JTAG_TABLE.UPDATE_IR = {'RUNTEST','SELECT_DR'};

local jtag_status = 'LRESET';
local jtag_dr = 0;
local jtag_ir = 0;
local jtag_ir_bitpos = 0;
local jtag_dr_bitpos = 0;

local JTAG_STATUS_PROC = function(tms,tdi)
    --io.stderr:write(tostring(jtag_status)..'\n');
    assert(JTAG_TABLE[jtag_status]);

    local v = 0; if(tdi)then v = 1; end;
    local pat = '%s:%08x(%d)\n';

    -- if(jtag_dr_bitpos>=33)then
    --     io.stderr:write(format(pat,'SHIFT-DR',jtag_dr));
    --     jtag_dr_bitpos = 0;
    -- end

    if(jtag_status=='SHIFT_DR')then
        jtag_dr = jtag_dr + (v<<jtag_dr_bitpos);
        jtag_dr_bitpos = jtag_dr_bitpos + 1;
    elseif(jtag_status=='SHIFT_IR')then
        jtag_ir = jtag_ir + (v<<jtag_ir_bitpos);
        jtag_ir_bitpos = jtag_ir_bitpos + 1;
    end


    if(jtag_status=='UPDATE_DR')then
        io.stderr:write(format(pat,'UPDATE-DR',jtag_dr,jtag_dr_bitpos));
        jtag_dr = 0;
        jtag_dr_bitpos = 0;
    elseif(jtag_status=='UPDATE_IR')then
        io.stderr:write(format(pat,'UPDATE-IR',jtag_ir,jtag_ir_bitpos));
        jtag_ir = 0;
        jtag_ir_bitpos = 0;
    end

    if(tms)then
        jtag_status = JTAG_TABLE[jtag_status][2];
    else
        jtag_status = JTAG_TABLE[jtag_status][1];
    end
end


local tck = '';
local tms = '';
local tdi = '';

local old_tck;
local old_tms;
local old_tdi;

local byte_shift = 0;
local old_hex = '';
for w in string.gmatch(str,'(%x%x )')do
    local hex = w;
    w = tonumber('0x'..w);

    if(byte_shift>0)then
        tck = tck..'|';
        tms = tms..'|';
        tdi = tdi..'|';
        byte_shift = byte_shift - 1;
        for i=0,7 do
            if((w & 1)~=0)then old_tdi = '^';else old_tdi = '_'; end
            w = w >> 1;

            old_tck = '_';

            tck = tck..old_tck;
            tms = tms..old_tms;
            tdi = tdi..old_tdi;

            old_tck = '^';
            JTAG_STATUS_PROC(old_tms=='^',old_tdi=='^');

            tck = tck..old_tck;
            tms = tms..old_tms;
            tdi = tdi..old_tdi;
        end
    else
        if((w & 0x80)==0)then
            if((w & 0x1)~=0)then  old_tck = '^'; else old_tck='_'; end
            if((w & 0x2)~=0)then  old_tms = '^'; else old_tms='_'; end
            if((w & 0x10)~=0)then old_tdi = '^'; else old_tdi='_'; end
        end
        if((w & 0x80)~=0)then
            byte_shift = w & 0x3f;
            tck = tck..'(' .. hex .. ')';
            tms = tms..'(' .. hex .. ')';
            tdi = tdi..'(' .. hex .. ')';
        end
        -- if(w==0xbf)then
        --     byte_shift = 0;
        -- end

        if(old_tck=='^')then
            --上升沿
            JTAG_STATUS_PROC(old_tms=='^',old_tdi=='^');
        end

        if((w&0x80)==0)then
            tck = tck..old_tck;
            tms = tms..old_tms;
            tdi = tdi..old_tdi;
        end
    end
    old_hex = hex;
end

print("tck:",tck);
print("tms:",tms);
print("tdi:",tdi);
